import java.lang.reflect.*;

/**
 * @User: vitobo
 * @Date: 2024-07-03
 * @Description:
 */
public class ReflectClassDemo {
    // 通过反射创建一个对象
    public static void reflectNewInstance(){
        try{
            Class<?> c3 = Class.forName("Student");
            Student student = (Student) c3.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor(){
        try{
            Class<?> c3 = Class.forName("Student");
            Constructor<?> constructor =
                    c3.getDeclaredConstructor(String.class, int.class);

            //设置为true后可修改访问权限
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("bobo", 25);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有属性
    public static void reflectPrivateField(){
        try{
            Class<?> c3 = Class.forName("Student");
            Field field = c3.getDeclaredField("name");
            field.setAccessible(true);

            Student student = (Student) c3.newInstance();
            field.set(student, "wenxin");
            System.out.println(student);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    // 反射私有方法
    public static void reflectPrivateMethod(){
        try{
            Class<?> c3 = Class.forName("Student");
            Method method = c3.getDeclaredMethod("function",  String.class);
            method.setAccessible(true);

            Student student = (Student) c3.newInstance();
            method.invoke(student, "测试方法,传参数");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        // reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
